package com.example.webdriveddd.repository;

import com.example.webdriveddd.domain.FileInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;
import java.util.Set;

public interface FileRepository extends JpaRepository<FileInfo, Integer> {
    @Query("select f from FileInfo f join f.fileTags t where t.id = :tagId and f.user.id= :userId")
    Set<FileInfo> findFileInfosByUserIdAndTagId(@Param("userId") Integer userId,@Param("tagId") Integer tagId);
    @Query("select f from FileInfo f join f.fileTags t where t.tag=:tag and t.user.username=:username and f.user.username=:username")
    Set<FileInfo> findFileInfosByUsernameAndTag(@Param("username") String username,@Param("tag") String tag);
    @Query("select f from FileInfo f where f.user.id=:userId and f.parentFile is null and f.fileName='/'")
    Optional<FileInfo> findRootFile(@Param("userId") int userId);
    Set<FileInfo> searchByUserIdAndFileNameContains(int userId,String keyword);
    @Query("select f from FileInfo f join f.fileTags t where t.user.id=:userId and t.tag like %:keyword%")
    Set<FileInfo> searchByUserIdAndTagContains(@Param("userId") int userId,@Param("keyword") String keyword);
    @Query("select f from FileInfo f where f.user.username=:username and f.parentFile is null and f.fileName='/'")
    Optional<FileInfo> findRootFile(@Param("username") String username);
}
